using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using Common;
using Common.Enum;
using Define;
using SProject.BasicInformation;
using SProject.Paramter;
using SProject.Rule;

namespace ProjectView.RuleView
{
	public partial class UCRuleTable : UserControl, IComponentConnector, IStyleConnector
	{
		public static readonly DependencyProperty IsReadOnlyProperty = DependencyProperty.Register("IsReadOnly", typeof(bool), typeof(UCRuleTable), new UIPropertyMetadata(false));

		public static readonly DependencyProperty ConclusionVisibleProperty = DependencyProperty.Register("ConclusionVisible", typeof(bool), typeof(UCRuleTable), new UIPropertyMetadata(true, OnConclusionVisibleChanged));

		public static readonly DependencyProperty ShowAnyTargetProperty = DependencyProperty.Register("ShowAnyTarget", typeof(bool), typeof(UCRuleTable), new UIPropertyMetadata(false, OnShowAnyTargetChanged));

		public static readonly DependencyProperty BasicOptionProperty = DependencyProperty.Register("BasicOption", typeof(BasicOption), typeof(UCRuleTable), new UIPropertyMetadata(null, OnBasicOptionChanged));

		private BasicOption _NewBasicOption = new BasicOption();

		public static readonly DependencyProperty SimpleRuleProperty = DependencyProperty.Register("SimpleRule", typeof(SimpleRule), typeof(UCRuleTable), new UIPropertyMetadata(null, OnSimpleRuleChanged));

		public static readonly DependencyProperty SampleListProperty = DependencyProperty.Register("SampleList", typeof(List<KeyValuePair>), typeof(UCRuleTable), new UIPropertyMetadata(null));

		public bool IsReadOnly
		{
			get
			{
				return (bool)GetValue(IsReadOnlyProperty);
			}
			set
			{
				SetValue(IsReadOnlyProperty, value);
			}
		}

		public bool ConclusionVisible
		{
			get
			{
				return (bool)GetValue(ConclusionVisibleProperty);
			}
			set
			{
				SetValue(ConclusionVisibleProperty, value);
			}
		}

		public bool ShowAnyTarget
		{
			get
			{
				return (bool)GetValue(ShowAnyTargetProperty);
			}
			set
			{
				SetValue(ShowAnyTargetProperty, value);
			}
		}

		public BasicOption BasicOption
		{
			get
			{
				return (BasicOption)GetValue(BasicOptionProperty);
			}
			set
			{
				SetValue(BasicOptionProperty, value);
			}
		}

		public BasicOption NewBasicOption
		{
			get
			{
				return _NewBasicOption;
			}
			set
			{
				_NewBasicOption = value;
			}
		}

		public SimpleRule SimpleRule
		{
			get
			{
				return (SimpleRule)GetValue(SimpleRuleProperty);
			}
			set
			{
				SetValue(SimpleRuleProperty, value);
			}
		}

		public List<KeyValuePair> SampleList
		{
			get
			{
				return (List<KeyValuePair>)GetValue(SampleListProperty);
			}
			set
			{
				SetValue(SampleListProperty, value);
			}
		}

		private static void OnConclusionVisibleChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
			UCRuleTable uCRuleTable = sender as UCRuleTable;
			if (uCRuleTable != null)
			{
				uCRuleTable.RefreshColumnConclusionVisible();
			}
		}

		private static void OnShowAnyTargetChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
			UCRuleTable uCRuleTable = sender as UCRuleTable;
			if (uCRuleTable != null)
			{
				uCRuleTable.UpdataNewBasicOption();
			}
		}

		private static void OnBasicOptionChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
			UCRuleTable uCRuleTable = sender as UCRuleTable;
			if (uCRuleTable != null)
			{
				uCRuleTable.UpdataNewBasicOption();
				uCRuleTable.RegisterBasicOptionEvent();
			}
		}

		private void RegisterBasicOptionEvent()
		{
			if (BasicOption != null)
			{
				BasicOption.Items.CollectionChanged += Items_CollectionChanged;
			}
		}

		private void Items_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
		{
			UpdataNewBasicOption();
		}

		private void UpdataNewBasicOption()
		{
			if (BasicOption == null)
			{
				return;
			}
			NewBasicOption.Items.Clear();
			if (ShowAnyTarget)
			{
				NewBasicOption.AddBasicOptionItem(new TargetItem
				{
					TubeNo = -1,
					ChannelNo = -1,
					Name = Utility.GetMessage("Any"),
					TubeName = null,
					TargetIndex = 1
				});
			}
			foreach (BasicOptionItem item in BasicOption.Items)
			{
				NewBasicOption.Items.Add(item);
			}
		}

		private static void OnSimpleRuleChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
		}

		public UCRuleTable()
		{
			InitializeComponent();
		}

		private void RefreshColumnConclusionVisible()
		{
			for (int i = dataGrid.Columns.Count - 2; i < dataGrid.Columns.Count; i++)
			{
				dataGrid.Columns[i].Visibility = ((!ConclusionVisible) ? Visibility.Collapsed : Visibility.Visible);
			}
		}

		public List<object> GetSelectedItem()
		{
			return (from s in dataGrid.SelectedCells
				where s.Item != null
				select s.Item).Distinct().ToList();
		}

		public bool Validate()
		{
			if (SimpleRule == null)
			{
				return false;
			}
			int index = -1;
			int i = -1;
			StringBuilder stringBuilder = new StringBuilder();
			if (SimpleRule.Use)
			{
				for (i = 0; i < SimpleRule.Items.Count; i++)
				{
					RuleItem item = SimpleRule.Items[i];
					if (string.IsNullOrEmpty(item.TargetName) || (!item.IsAnyTarget && BasicOption.Items.Where((BasicOptionItem s) => s.TargetName == item.TargetName).Count() == 0))
					{
						index = 1;
						stringBuilder.Append(Utility.GetMessage("msg_not_input_target"));
						break;
					}
					if (item.CalcResult == ECalcResult.None)
					{
						index = 4;
						stringBuilder.Append(Utility.GetMessage("msg_calcresult_invalid"));
						break;
					}
					if (item.RightOperator == EOperator.Null && item.LeftOperator == EOperator.Null)
					{
						index = 3;
						stringBuilder.Append(Utility.GetMessage("msg_operator_invalid"));
						break;
					}
					if ((item.RightOperator == EOperator.Equal || item.RightOperator == EOperator.UnEqual) && item.LeftOperator != 0)
					{
						index = 3;
						stringBuilder.Append(Utility.GetMessage("msg_operator_invalid"));
						break;
					}
					if ((item.LeftOperator == EOperator.LessThan || item.LeftOperator == EOperator.EqualLessThan) && ConstValue.IsVoidValue(item.LeftValue))
					{
						index = 2;
						stringBuilder.Append(Utility.GetMessage("msg_number_invalid"));
						break;
					}
					if (!ConstValue.IsVoidValue(item.LeftValue) && item.LeftOperator == EOperator.Null)
					{
						index = 3;
						stringBuilder.Append(Utility.GetMessage("msg_operator_invalid"));
						break;
					}
					if ((item.RightOperator == EOperator.LessThan || item.RightOperator == EOperator.EqualLessThan) && ConstValue.IsVoidValue(item.RightValue))
					{
						index = 6;
						stringBuilder.Append(Utility.GetMessage("msg_number_invalid"));
						break;
					}
					if (!ConstValue.IsVoidValue(item.RightValue) && item.RightOperator == EOperator.Null)
					{
						index = 5;
						stringBuilder.Append(Utility.GetMessage("msg_operator_invalid"));
						break;
					}
					if ((item.LeftOperator == EOperator.LessThan || item.LeftOperator == EOperator.EqualLessThan) && (item.RightOperator == EOperator.LessThan || item.RightOperator == EOperator.EqualLessThan) && item.RightValue <= item.LeftValue)
					{
						index = 2;
						stringBuilder.Append(Utility.GetMessage("msg_number_invalid"));
						break;
					}
				}
			}
			if (stringBuilder.Length > 0)
			{
				dataGrid.UnselectAllCells();
				MessageBox.Show(stringBuilder.ToString());
				dataGrid.CurrentColumn = dataGrid.Columns[index];
				dataGrid.CurrentItem = SimpleRule.Items[i];
				dataGrid.BeginEdit();
				return false;
			}
			return true;
		}

		private void TextBlock_TargetName_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
		{
			TextBlock textBlock = sender as TextBlock;
			if (textBlock != null && textBlock.DataContext is RuleItem && NewBasicOption != null)
			{
				RuleItem item = textBlock.DataContext as RuleItem;
				item.BasicOptionItem = NewBasicOption.Items.Where((BasicOptionItem s) => s.TargetName == item.TargetName).FirstOrDefault();
			}
		}

		private void ComboBox_Target_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			ComboBox comboBox = sender as ComboBox;
			if (comboBox != null && comboBox.DataContext is RuleItem && NewBasicOption != null && NewBasicOption.Items.Count > 0)
			{
				RuleItem ruleItem = comboBox.DataContext as RuleItem;
				ruleItem.IsAnyTarget = ShowAnyTarget && NewBasicOption.Items[0] == ruleItem.BasicOptionItem;
			}
		}

		private void dataGrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
		{
			if (IsReadOnly)
			{
				e.Cancel = true;
			}
		}
	}
}
